const db = require('../mysql/index')

exports.getMallInfo = (req, res, next) => {
    console.log(req.query, 'req.query');
    const { params } = req.query //params: {xxx,xxx}
    if (params) {
        if (params.mallCategory && !params.mallVersion) {
            const sql2 = 'select * from mallInfo where mallCategory = ? '
            db.query(sql2, params.mallCategory, (err, result) => {
                if (result) {
                    res.send({
                        msg: '查询成功',
                        code: 200,
                        data: result
                    })
                } else {
                    res.send({
                        msg: err,
                        code: 400
                    })
                }
            })
        } else if (params.mallVersion && !params.mallCategory) {
            const sql3 = 'select * from mallInfo where mallVersion = ?'
            db.query(sql3, params.mallVersion, (err, result) => {
                if (result) {
                    res.send({
                        msg: '查询成功',
                        code: 200,
                        data: result
                    })
                } else {
                    res.send({
                        msg: err,
                        code: 400
                    })
                }
            })
        } else if (params.mallCategory && params.mallVersion) {
            const sql4 = 'select * from mallInfo where mallCategory = ? and mallVersion = ?'
            db.query(sql4, [params.mallCategory, params.mallVersion], (err, result) => {
                if (result) {
                    res.send({
                        msg: '查询成功',
                        code: 200,
                        data: result
                    })
                } else {
                    res.send({
                        msg: err,
                        code: 400
                    })
                }
            })

        } else {
            const sql1 = 'select * from mallInfo'
            db.query(sql1, (err, result) => {
                if (result) {
                    res.send({
                        msg: '查询成功',
                        code: 200,
                        data: result
                    })
                } else {
                    res.send({
                        msg: err,
                        code: 400
                    })
                }
            }
            )
        }

    } else {
        const sql1 = 'select * from mallInfo'
        db.query(sql1, (err, result) => {
            if (result) {
                res.send({
                    msg: '查询成功',
                    code: 200,
                    data: result
                })
            } else {
                res.send({
                    msg: err,
                    code: 400
                })
            }
        }
        )
    }

}

exports.addMallInfo = (req, res, next) => {
    const { mallName, mallCategory, mallVersion, mallParam, mallCost, mallPic } = req.body
    const sql = 'insert into mallInfo (mallName,mallCategory,mallVersion,mallCost,mallPic,mallParam) values (?,?,?,?,?,?)'
    db.query(sql, [mallName, mallCategory, mallVersion, mallCost, mallPic, mallParam], (err, result) => {
        if (result) {
            res.send({
                msg: '添加成功',
                code: 200
            })
        } else {
            res.send({
                msg: err,
                code: 400
            })
        }
    })
}

exports.deleteMallInfo = (req, res, next) => {
    const { id } = req.body
    const sql = 'delete from mallInfo where id = ?'
    db.query(sql, id, (err, result) => {
        if (result) {
            res.send({
                msg: '删除成功',
                code: 200
            })
        } else {
            res.send({
                msg: err,
            })
        }
    })
}

// 修改库存接口
exports.changeMallRepertory = (req, res, next) => {
    const { id, mallRepertory, type } = req.body
    let sql2 = 'select * from mallInfo where id = ?'
    db.query(sql2, id, (err, result) => {
        console.log(result, 'result');
        let num = 0
        if (type == 'put') {
            num = result[0].mallRepertory + mallRepertory
            let sql1 = 'update mallInfo set mallRepertory = ? where id = ?'
            db.query(sql1, [num, id], (err, result) => {
                if (result) {
                    res.send({
                        msg: '修改成功',
                        code: 200
                    })
                } else {
                    res.send({
                        msg: err,
                    })
                }
            })
        } else if (type == 'delivery') {
            num = result[0].mallRepertory - mallRepertory
            if (num > 0) {
                let sql2 = 'update mallInfo set mallRepertory = ? where id = ?'
                db.query(sql2, [num, id], (err, result) => {
                    if (result) {
                        res.send({
                            msg: '修改成功',
                            code: 200
                        })
                    } else {
                        res.send({
                            msg: err,
                        })
                    }
                })
            } else {
                res.send({
                    msg: '库存不足',
                    code: 400
                })
            }
        }
    })
    // let sql1 = 'update mallInfo set mallRepertory = ? where id = ?'
    // db.query(sql1, [mallRepertory, id], (err, result) => {
    //     if (result) {
    //         res.send({
    //             msg: '修改成功',
    //             code: 200
    //         })
    //     } else {
    //         res.send({
    //             msg:err,
    //         })
    //     }
    // })
}

// 首页库存展示
exports.showMallCircle = (req, res, next) => {
    let sql = 'select * from mallInfo'
    let circleData = []
    db.query(sql, (err, result) => {
        result.map((item) => {
            circleData.push({
                name: item.mallName,
                value: item.mallRepertory
            })
        })
        res.send({
            msg: '获取数据成功',
            code: 200,
            data: circleData
        })
    })
}

// 首页图表数据写死
exports.getChartsData = (req, res, next) => {
    let tableData = [
        {
            name: '油烟机',
            todayBuy: 300,
            monthBuy: 2000,
            totalBuy: 2300
        },
        {
            name: '净水器',
            todayBuy: 300,
            monthBuy: 2000,
            totalBuy: 2300
        },
        {
            name: '消毒柜',
            todayBuy: 300,
            monthBuy: 2000,
            totalBuy: 2300
        }
    ]
    let userData = [
        {
            date: '周一',
            new: 5,
            active: 200
        },
        {
            date: '周二',
            new: 5,
            active: 200
        },
        {
            date: '周三',
            new: 12,
            active: 200
        },
        {
            date: '周四',
            new: 60,
            active: 200
        },
        {
            date: '周五',
            new: 65,
            active: 200
        },
        {
            date: '周六',
            new: 53,
            active: 200
        },
        {
            date: '周七',
            new: 33,
            active: 200
        }
    ]
    let orderData = {
        date: ['20240701', '20240702', '20240703', '20240704', '20240705', '20240706', '20240707'],
        data: [
            {
                '油烟机': 2000,
                '消毒柜': 1500,
                '净水器': 3000
            },
            {
                '油烟机': 2000,
                '消毒柜': 1500,
                '净水器': 3130
            },
            {
                '油烟机': 2130,
                '消毒柜': 1400,
                '净水器': 1400
            },
            {
                '油烟机': 23460,
                '消毒柜': 1500,
                '净水器': 3140
            },
            {
                '油烟机': 2840,
                '消毒柜': 1800,
                '净水器': 3240
            },
            {
                '油烟机': 2150,
                '消毒柜': 1500,
                '净水器': 3000
            },
            {
                '油烟机': 2180,
                'vivo': 1100,
                '净水器': 3010
            },
        ]
    }
    res.send({
        msg: '获取数据成功',
        code: 200,
        data: {
            tableData,
            userData,
            orderData
        }
    })
}